package com.zcs.messagerelayer.utils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by chongshengzhang on 2017/6/25.
 */

public class ApiRelayerManager {
    /**
     * 发送短信至目标API 接口
     *
     * @param dataManager
     * @param content     短信内容
     * @param mobile      短信手机号
     */
    public static void relayApi(NativeDataManager dataManager, final String content, final String mobile) {
        String url = dataManager.getApiUrl();
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new RetryIntercepter(3))
                .build();

        FormBody body = new FormBody.Builder()
                .add("mobile", mobile)
                .add("content", content)
                .build();
        Request request = new Request.Builder().post(body).url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            //请求失败时调用
            @Override
            public void onFailure(Call call, IOException e) {
                LogToFile.d("发送失败",
                        "手机号：" + mobile + "[&]" +
                             "短信内容：" + content + "[&]" +
                             "错误：" + e.getMessage());
            }

            //请求成功时调用
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                LogToFile.d("发送成功",
                        "手机号：" + mobile + "[&]" +
                        "短信内容：" + content + "[&]" +
                        "服务端返回：" + response.body().string());
            }
        });
    }

}
